<?php


namespace app\admin\controller;


use app\common\model\CouponReceive;

class Coupon extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('coupon/couponlist')
     * 优惠券列表
     */
    public function couponlist()
    {
//        // 获取搜索条件
        $get['coupon_name'] = input('get.coupon_name');

        $model = new \app\common\model\Coupon();
        $where[] = ['is_del','=',1];
        if($get['coupon_name']){
            $where[] = ['coupon_name','like',"%".$get['coupon_name']."%"];
        }
        $rs_data = $model->where($where)->paginate($this->limit);
//        var_dump($rs_data->toArray());die;
        $tvar = array(
            "title" => "消费券管理列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('coupon/couponlist');
    }


    /**
     * @route('coupon/boxecoupon')
     * 添加编辑消费券
     */
    public function Boxecoupon()
    {
        $model = new \app\common\model\Coupon();
        $rmodel = new CouponReceive();
        if(request()->isAjax()){
            $scopetime = input("post.scope_time/s");
            $scope = explode(" - ",$scopetime);
//            var_dump($scope);die;
            $coupon_id = input("post.coupon_id/d",0);
            $param = [
                "coupon_name" => input("post.coupon_name/s"),
                "type" => 2,
                "discount" => input("post.discount/f"),
                "fullmoney" => input("post.fullmoney/f"),
                "send_stime" => strtotime($scope[0]),
                "send_etime" => strtotime($scope[1]),
                "coupon_number" => input("post.coupon_number/f"),
                "status" => input("post.status/f"),
                "admin_id" => $this->_userid,
                "shop_id" => $this->_shopid
            ];
//            var_dump($param);die;
            $model->startTrans();
            if($coupon_id == 0){
                $rs = $model->edit($param);
            } else {
                $rs = $model->edit($param,$coupon_id);
                $rwhere[] = ["coupon_id",'=',$coupon_id];
                $rwhere[] = ["is_use","=",1];
                $updata = [
                    'is_use' => 3,
                ];
                $rmodel->allowField(true)->save($updata, $rwhere);
            }
            if($rs === false){
                $model->rollback();
                $this->error($model->getError());
            }
            $model->commit();
            $this->success('操作成功','/coupon/couponlist');
        }

        $coupon_id = $_GET['coupon_id'];
        $coupon = null;
        $tvar = array(
            "title" => "添加消费券",
            "time"  => time(),
            "coupon_id"   => $coupon_id,
        );
        if($coupon_id != 0){
            $tvar["title"] = "编辑消费券";
            $coupon = $model->where('coupon_id','=',$coupon_id)->where('is_del','=',1)->find();
            if(!$coupon){
                $this->error('不存的消费券','/coupon/couponlist');
            }
        }

        $this->assign("data", $tvar);
        $this->assign("coupon", $coupon);
        return $this->fetch('coupon/boxecoupon');
    }


    /**
     * @route('coupon/delcoupon')
     * 商品库存删除
     */
    public function DelCoupon()
    {
        $couponid = input('coupon_id');
        $model = new \app\common\model\Coupon();
        $where['coupon_id'] = $couponid;
        $where['is_del'] = 1;
        $coupon = $model->where($where)->find();
//        var_dump($stock);die;
        if(!$coupon){
            $this->error('不存在的消费券');
        }
        $param['is_del'] = 2;
        $rs2 = $model->edit($param,$couponid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }
}